﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace QLyHocSinhTHPT
{
    public partial class GiaoVien : DevComponents.DotNetBar.Office2007Form
    {
        public GiaoVien()
        {
            InitializeComponent();
        }

        public SqlConnection GetConnect()
        {
            return new SqlConnection(@"Data Source=HOANGNGOC-PC\HOANGNGOC;Initial Catalog=QLyHocSinhTHPT;Integrated Security=True");
        }

        private void label1_Click(object sender, EventArgs e)
        {
        
        }

        private void dataGridViewX1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
        
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            if (txtmagv.Text.Trim().Equals(" "))
            {
                MessageBox.Show("ban phai nhap ma giao vien");
                txtmagv.Focus();
                return;
            }
            GVien gv = new GVien();
            gv.MaGV = txtmagv.Text;
            gv.HoDem = txthodem.Text;
            gv.TenGV = txttengv.Text;
            gv.NgaySinh = dtNS.Value;
            gv.GioiTinh = int.Parse(txtgioitinh.Text);
            gv.DiaChi = txtdiachi.Text;
            gv.SDT = txtsodt.Text;
            gv.MaMH = txtmamh.Text;

            try
            {
                GiaoVien_Insert(gv);
                dgvgiaovien.DataSource = GiaoVien_SelectAll();
            }
            catch
            {
                MessageBox.Show("ko them dc");
            }
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            if (txtmagv.Text.Trim().Equals(" "))
            {
                MessageBox.Show("ban phai nhap ma giao vien");
                txtmagv.Focus();
                return;
            }
            GVien gv = new GVien();
            gv.MaGV = txtmagv.Text;
            gv.HoDem = txthodem.Text;
            gv.TenGV = txttengv.Text;
            gv.NgaySinh = dtNS.Value;
            gv.GioiTinh = int.Parse(txtgioitinh.Text);
            gv.DiaChi = txtdiachi.Text;
            gv.SDT = txtsodt.Text;
            gv.MaMH = txtmamh.Text;

            
           
                GiaoVien_Update(gv);
                dgvgiaovien.DataSource = GiaoVien_SelectAll();
            
           
          
          
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            if (txtmagv.Text.Trim().Equals(" "))
            {
                MessageBox.Show("ban phai nhap ma giao vien");
                txtmagv.Focus();
                return;
            }

            string magv;
            magv = txtmagv.Text;
            GiaoVien_Delete(magv);
            dgvgiaovien.DataSource = GiaoVien_SelectAll();
        }

        private void btnfind_Click(object sender, EventArgs e)
        {

        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void txttimkiem_TextChanged(object sender, EventArgs e)
        {
            GVien gv = new GVien();
            gv.MaGV = txttimkiem.Text;
            gv.TenGV = txttimkiem.Text;
            dgvgiaovien.DataSource = GiaoVien_Search(gv);
        }

        private void dgvgiaovien_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int RowIndex = e.RowIndex;
            txtmagv.Text = dgvgiaovien.Rows[RowIndex].Cells[0].Value.ToString();
            txthodem.Text = dgvgiaovien.Rows[RowIndex].Cells[1].Value.ToString();
            txttengv.Text = dgvgiaovien.Rows[RowIndex].Cells[2].Value.ToString();
            dtNS.Value = DateTime.Parse(dgvgiaovien.Rows[RowIndex].Cells[3].Value.ToString());
            txtgioitinh.Text = dgvgiaovien.Rows[RowIndex].Cells[4].Value.ToString();
            txtdiachi.Text = dgvgiaovien.Rows[RowIndex].Cells[5].Value.ToString();
            txtsodt.Text = dgvgiaovien.Rows[RowIndex].Cells[6].Value.ToString();
            txtmamh.Text = dgvgiaovien.Rows[RowIndex].Cells[7].Value.ToString();
        }

        private void GiaoVien_Load(object sender, EventArgs e)
        {
            dgvgiaovien.DataSource = GiaoVien_SelectAll();
        }

        public DataTable GiaoVien_SelectAll()
        {
            SqlConnection con = GetConnect();
            con.Open();
            string sql = "select * from tblGiaoVien";
            SqlDataAdapter adap = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            adap.Fill(dt);
            return dt;
        }

        public void GiaoVien_Insert(GVien gv)
        {
            SqlConnection con = GetConnect();
            con.Open();
            SqlCommand cmd = new SqlCommand("sp_GiaoVien_Insert", con);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter("@magv", gv.MaGV));
            cmd.Parameters.Add(new SqlParameter("@hodem", gv.HoDem));
            cmd.Parameters.Add(new SqlParameter("@tengv", gv.TenGV));
            cmd.Parameters.Add(new SqlParameter("@ngaysinh", gv.NgaySinh));
            cmd.Parameters.Add(new SqlParameter("@gioitinh", gv.GioiTinh));
            cmd.Parameters.Add(new SqlParameter("@diachi", gv.DiaChi));
            cmd.Parameters.Add(new SqlParameter("@sdt", gv.SDT));
            cmd.Parameters.Add(new SqlParameter("@mamh", gv.MaMH));

            cmd.ExecuteNonQuery();
            con.Close();
        }

        public void GiaoVien_Update(GVien gv)
        {
            SqlConnection con = GetConnect();
            con.Open();
            SqlCommand cmd = new SqlCommand("sp_GiaoVien_Update", con);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter("@magv", gv.MaGV));
            cmd.Parameters.Add(new SqlParameter("@hodem", gv.HoDem));
            cmd.Parameters.Add(new SqlParameter("@tengv", gv.TenGV));
            cmd.Parameters.Add(new SqlParameter("@ngaysinh", gv.NgaySinh));
            cmd.Parameters.Add(new SqlParameter("@gioitinh", gv.GioiTinh));
            cmd.Parameters.Add(new SqlParameter("@diachi", gv.DiaChi));
            cmd.Parameters.Add(new SqlParameter("@sdt", gv.SDT));
            cmd.Parameters.Add(new SqlParameter("@mamh", gv.MaMH));

            cmd.ExecuteNonQuery();
            con.Close();
        }

        public void GiaoVien_Delete(string magv)
        {
            SqlConnection con = GetConnect();
            con.Open();
            SqlCommand cmd = new SqlCommand("sp_GiaoVien_Delete", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@magv", magv));
            cmd.ExecuteNonQuery();
            con.Close();
        }

        public DataTable GiaoVien_Search(GVien gv)
        {
            SqlConnection con = GetConnect();
            con.Open();
            SqlCommand cmd = new SqlCommand("sp_GiaoVien_Search", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@magv", gv.MaGV));
            cmd.Parameters.Add(new SqlParameter("@tengv", gv.TenGV));
            SqlDataAdapter adap = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adap.Fill(dt);
            return dt;
        }
    }
}
